package com.hxf.dao.food.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.GlobalConstant;
import com.hxf.common.PublicUtil;
import com.hxf.dao.BaseDaoImpl;
import com.hxf.dao.food.MealMenuDao;
import com.hxf.dto.MealMenuDto;
import com.hxf.dto.MealMenuTypeDto;
import com.hxf.dto.MenuDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.DaoLevelException;
import com.hxf.web.vo.MealMenuVo;
import com.hxf.web.vo.OrderDetailVo;

@SuppressWarnings("unchecked")
@Repository("mealMenuDao")
public class MealMenuDaoImpl extends BaseDaoImpl implements MealMenuDao {

	@Override
	public ReturnData<MealMenuDto> findByMealMenu(MealMenuDto mealMenuDto, int targetPage, int pageSize) throws DaoLevelException {
		String sql = "";
		if(null != mealMenuDto){
			
			if (mealMenuDto != null && mealMenuDto.getRestaurantId() != null) {
				sql += "  and restaurantId = " + mealMenuDto.getRestaurantId();
			}
			//编号查询
			if(PublicUtil.isNotNull(mealMenuDto.getId())){
				sql += " and id = " + mealMenuDto.getId();
			}
			
			//菜肴查询
			if(PublicUtil.isNotNullAndEmpty(mealMenuDto.getMenuName())){
				sql += " and menuName like '%"+mealMenuDto.getMenuName().trim()+"%' ";
			}
			
			//菜品类别
			if(PublicUtil.isNotNull(mealMenuDto.getMenuTypeId())){
				sql += " and menuTypeId = " + mealMenuDto.getMenuTypeId();
			}
			
			if(PublicUtil.isNotNull(mealMenuDto.getRestaurantId())){
				sql += " and restaurant_id = " + mealMenuDto.getRestaurantId();
			}
		}
		
		return super.findBy("from MealMenuDto where 1 =1 " + sql , targetPage, pageSize);
	}
	
	@Override
	public List<MealMenuDto> findByMealMenu(MealMenuDto mealMenuDto) throws DaoLevelException {
		String sql = "";
		if (mealMenuDto != null) {
			if(PublicUtil.isNotNull(mealMenuDto.getRestaurantId())){
				sql = " and restaurantId = " + mealMenuDto.getRestaurantId();
			}
		}
		return super.findBy("from MealMenuDto where 1=1 " + sql);
	}
	
	@Override
	public List<OrderDetailVo> findByMealMenuPreferential(MealMenuDto mealMenuDto) throws DaoLevelException {
		String sql = "";
		if (mealMenuDto != null && mealMenuDto.getRestaurantId() != null) {
			sql = " and restaurantId = " + mealMenuDto.getRestaurantId();
		}
		List list = super.exeSQLList("select id,price,menu_name,preferential_price,discount from rk_meal_menu rmm,rk_meal_preferential rmp where rmm.id=rmp.meal_menu_id" + sql);
		
		List<OrderDetailVo> orderDetailList =  new ArrayList<OrderDetailVo>();
		OrderDetailVo menuDto = null;
		if (list != null && list.size() > 0) {
			for (int i = 0;i < list.size(); i++) {
				Object [] o = (Object[])list.get(i);
				menuDto = new OrderDetailVo();
				menuDto.setId(null==o[0]?null:Integer.parseInt(o[0].toString()));
				menuDto.setName(null==o[1]?null:o[1].toString());
				menuDto.setPrice(null==o[2]?null:Double.parseDouble(o[2].toString()));
				menuDto.setPreferentialPrice(null==o[3]?null:Double.parseDouble(o[3].toString()));
				menuDto.setDiscont(null==o[4]?null:Double.parseDouble(o[4].toString()));
				orderDetailList.add(menuDto);
			}
		}
		return orderDetailList;  
	}
	
	@Override
	public List<MealMenuTypeDto> findByMealMenuType(MealMenuTypeDto mealMenuTypeDto) throws DaoLevelException {
		String sql = "";
		if(PublicUtil.isNotNullAndEmpty(mealMenuTypeDto.getMenuTypeName())){
			sql += " and menuTypeName = '"+mealMenuTypeDto.getMenuTypeName().trim()+"'";
		}
		if(PublicUtil.isNotNull(mealMenuTypeDto.getRestaurantId())){
			sql += " and restaurantId = " + mealMenuTypeDto.getRestaurantId();
		}
		return super.findBy("from MealMenuTypeDto WHERE 1=1 " + sql); 
	}

	@Override
	public ReturnData<MealMenuVo> findMealMenuByRestaurant(RestaurantDto restaurantDto, MealMenuDto mealMenuDto, int targetPage, int pageSize) throws DaoLevelException {
		String sql = ""; 
		if (restaurantDto != null) {
			//名称
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getName())){
				 sql +=" and name = '"+restaurantDto.getName()+"'";
			}
			
			//省份
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getProvince())){
				sql += " and province = '"+restaurantDto.getProvince().trim()+"' ";
			}
			
			//市
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getCity())){
				sql += " and city = '"+restaurantDto.getCity().trim()+"' ";
			}
				
			//区
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getDistrict())){
				sql += " and district like '"+restaurantDto.getDistrict().trim()+"'% ";
			}
			
			//街道
			if(PublicUtil.isNotNullAndEmpty(restaurantDto.getStreet())){
				sql += " and street like '"+restaurantDto.getDistrict().trim()+"'% ";
			}
		}
		
		if (mealMenuDto != null) {
			if (PublicUtil.isNotNullAndEmpty(mealMenuDto.getMenuName())) {
				sql += " and menuName like '"+mealMenuDto.getMenuName().trim()+"'% ";
			}
		}
		
		ReturnData result = super.exeSQLList("select rmm.id,menu_name,meal_menu_id,is_sell,introduce,images,order_by,price,taste,rmm.is_recommend,rms.name from rk_restaurant as res,rk_meal_menu rmm,rk_meal_style rms where res.id=rmm.restaurant_id and rms.id=rmm.menu_type_id" + sql, targetPage, pageSize);
		List list = result.getResultlist();
		
		if (list != null && list.size() > 0) {
			List<MealMenuVo> mmVoList = new ArrayList<MealMenuVo>();
			MealMenuVo mmVo =null;
			for (int i = 0;i < list.size(); i++) {
				Object [] o = (Object[])list.get(i);
				mmVo = new MealMenuVo();
				mmVo.setId(null==o[0]?null:Integer.parseInt(o[0].toString()));
				mmVo.setMenuName(null==o[1]?null:o[1].toString());
				mmVo.setMealMenuId(null==o[2]?null:o[2].toString());
				if (null != o[3]) {
					if (Boolean.parseBoolean(o[3].toString())) {
						mmVo.setIsSell(1);
					} else {
						mmVo.setIsSell(0);
					}
				}
				mmVo.setIntroduce(null==o[4]?null:o[4].toString()); 
				if (null != o[5] && !"".equals(o[5].toString())) {
					mmVo.setImageUrl(GlobalConstant.WEB_URL+GlobalConstant.PROJECT_API+o[5].toString());
				} else {
					mmVo.setImageUrl(" ");
				}
				mmVo.setOrderBy(null==o[6]?null:Integer.parseInt(o[6].toString()));
				mmVo.setPrice(null==o[7]?null:Double.parseDouble(o[7].toString()));
				if (null != o[8]) {
					if (Integer.parseInt(o[8].toString()) == MealMenuDto.TASTE_0) {
						mmVo.setTaste("不辣");
					}
					if (Integer.parseInt(o[8].toString()) == MealMenuDto.TASTE_1) {
						mmVo.setTaste("香辣");
					}
					if (Integer.parseInt(o[8].toString()) == MealMenuDto.TASTE_2) {
						mmVo.setTaste("香辣(可选)");
					}
					if (Integer.parseInt(o[8].toString()) == MealMenuDto.TASTE_3) {
						mmVo.setTaste("微辣");
					}

				}
				if (null != o[9]) {
					if (Boolean.parseBoolean(o[9].toString())) {
						mmVo.setIsRecommend(1);
					} else {
						mmVo.setIsRecommend(0);
					}
				}
				
				mmVo.setStyleName(null==o[10]?null:o[10].toString());
				mmVoList.add(mmVo);
			}
			result.setResultlist(mmVoList);
		}
		return result;
	}
	
/*	@Override
	public List<MealMenuDto> findByMealMenuByIds(String mealMenuIds) throws DaoLevelException {
		List list = super.exeSQLList("select id,menu_name,price from rk_tmp_table as tmp,rk_meal_menu as rmp where tmp.value1=rmp.id");
		List<MealMenuDto> mmDtoList = new ArrayList<MealMenuDto>();
		if (null != list && list.size() > 0) {
			MealMenuDto menuDto = null;
			for (int i = 0;i < list.size(); i++) { 
				menuDto = new MealMenuDto();
				Object[] obj = (Object[])list.get(i);
				menuDto.setId(null==obj[0]?null:Integer.parseInt(obj[0].toString()));
				menuDto.setMenuName(null==obj[1]?null:obj[1].toString());
				menuDto.setPrice(null==obj[2]?null:Double.parseDouble(obj[2].toString()));
				mmDtoList.add(menuDto);
			}
		}
		return mmDtoList;
	}*/
	
	@Override
	public List<MealMenuDto> findByMealMenuByIds(String mealMenuIds) throws DaoLevelException {
		 
		return  super.findBy("from MealMenuDto where id in (" + mealMenuIds + ")");
	}
}
